package com.lanlian.cayman.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.lanlian.cayman.R;
import com.lanlian.utillib.DateUtils;
import com.lanlian.utillib.DisplayUtil;
import com.lanlian.utillib.ToastUtils;

import java.util.Date;

/**
 * <pre>
 *     author : huxiaobo
 *     e-mail : huxiaobo@xlanlian.com
 *     date   : 2018/8/28 15:57
 *     desc   : 明细筛选框
 *     version: 1.0
 * </pre>
 */
public class FundFlowFilterWindow extends Dialog implements View.OnClickListener {

    private TextView mTvBeginTime;
    private TextView mTvEndTime;
    private TextView mTvFinish;
    private TextView mTvCancel;
    private View mView;

    private Context mContext;
    private long mBeginDate;
    private long mEndDate;
    private OnFilterListener mOnFilterListener;
    private DateChooseDialog mEndDateDialog;
    private DateChooseDialog mBeginDateDialog;

    public interface OnFilterListener {
        void onFilter(long beginDate, long endDate);
    }

    public FundFlowFilterWindow(Context context, OnFilterListener onFilterListener) {
        super(context, R.style.customDialog);
        this.mContext = context;
        this.mOnFilterListener = onFilterListener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.popup_fund_flow_filter);

        Window window = getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        window.setGravity(Gravity.TOP);
        lp.y = DisplayUtil.dip2px(mContext, 50);
        lp.width = DisplayUtil.getScreenWidth(mContext);
        lp.height = DisplayUtil.getScreenHeight(mContext) - lp.y - DisplayUtil.getStatusBarHeight(mContext);
        window.setAttributes(lp);
        window.setDimAmount(0f);
        window.setWindowAnimations(R.style.animFromTop);

        initView();
        setListener();
    }

    private void initView() {
        mTvBeginTime = findViewById(R.id.tv_begin_time);
        mTvEndTime = findViewById(R.id.tv_end_time);
        mTvFinish = findViewById(R.id.tv_finish);
        mTvCancel = findViewById(R.id.tv_cancel);
        mView = findViewById(R.id.view);
        mTvBeginTime.setHint(DateUtils.getDate(new Date()));
        mTvEndTime.setHint(DateUtils.getDate(new Date()));
    }

    private void setListener() {
        mTvCancel.setOnClickListener(this);
        mTvFinish.setOnClickListener(this);
        mTvBeginTime.setOnClickListener(this);
        mTvEndTime.setOnClickListener(this);
        mView.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.view:
            case R.id.tv_cancel:
                dismiss();
                break;
            case R.id.tv_finish:
                if (mBeginDate == 0 || mEndDate == 0) {
                    ToastUtils.show(mContext, "请选择时间");
                    return;
                }
                if (Math.abs(mBeginDate - mEndDate) / 1000 > 30 * 24 * 60 * 60) {
                    ToastUtils.show(mContext, "时间跨度不能超过30天");
                    return;
                }
                if (mBeginDate >= mEndDate) {
                    mOnFilterListener.onFilter(mEndDate, mBeginDate);
                } else {
                    mOnFilterListener.onFilter(mBeginDate, mEndDate);
                }
                dismiss();
                break;
            case R.id.tv_begin_time:
                if (mBeginDateDialog == null) {
                    mBeginDateDialog = new DateChooseDialog(mContext, new DateChooseDialog.OnConfirmListener() {
                        @Override
                        public void onConfirmClick(String timeStr, long time) {
                            mTvBeginTime.setText(timeStr);
                            mBeginDate = time;
                        }
                    });
                }
                mBeginDateDialog.showDialog();
                break;
            case R.id.tv_end_time:
                if (mEndDateDialog == null) {
                    mEndDateDialog = new DateChooseDialog(mContext, new DateChooseDialog.OnConfirmListener() {
                        @Override
                        public void onConfirmClick(String timeStr, long time) {
                            mTvEndTime.setText(timeStr);
                            mEndDate = time;
                        }
                    });
                }
                mEndDateDialog.showDialog();
                break;
            default:
                break;
        }
    }

}
